
import java.io.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
// readline thêm tag record
public class ReadHTML {

    public void readWriteFile() {
        File file = new File("C:\\input.html");
        StringBuffer contents = new StringBuffer();

        int count = 0;
        try {
            //use buffering, reading one line at a time
            //FileReader always assumes default encoding is OK!
            BufferedReader input = new BufferedReader(new FileReader(file));
            try {
                String line = null; //not declared within while loop
                        /*
                 * readLine is a bit quirky :
                 * it returns the content of a line MINUS the newline.
                 * it returns null only for the END of the stream.
                 * it returns an empty String if two newlines appear in a row.
                 */
                contents.append("<root>");
                while ((line = input.readLine()) != null) {
                    count++;
                    /*
                    pw.println("<record RecId=" + "\"" + count + "\"" + ">");
                    pw.println("<Infomation>" + s + "</Infomation>");
                    pw.println("</record>");
                    pw.close();
                     */
                    contents.append("<record RecId=" + "\"" + count + "\"" + ">");
                    contents.append("<Infomation>");
                    contents.append(line);
                    contents.append("</Infomation>");
                    contents.append("</record>");
                    contents.append(System.getProperty("line.separator"));
                }
                contents.append("</root>");
            } finally {
                input.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
///////////////////
         try {
            //Ghi file
            FileOutputStream fos = new FileOutputStream("Example.xml", true);
            PrintWriter pw = new PrintWriter(fos);
            pw.println(contents.toString());
            pw.close();

        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    ////////////////

        System.out.println(contents.toString());
    }

    public static void main(String[] args) {
        ReadHTML RHTML = new ReadHTML();
        RHTML.readWriteFile();

    }
}
